@extends('backend.layouts.base')
@section('title', '分类管理')
@section('content')
    <link rel="stylesheet" href="/static/css/backend/detail.css">
    <div class="all">
        <div id="app" v-cloak>
            <div class="vue-crumbs">
                <a @click="goParent()">分类管理</a> > 编辑分类
            </div>
            <el-form ref="form" :model="form" :rules="rules" label-width="15%">
                <div class="vue-head">
                    <div class="vue-main-title">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content">基本信息</div>
                    </div>
                    <div class="vue-main-form">
                        <el-form-item label="上级分类">
                            <el-select v-model="form.pid" filterable clearable placeholder="">
                                <el-option label="作为一级分类" :value="0"></el-option>
                                <el-option v-for="(item,index) in parent_list" :key="index" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="分类名称">
                            <el-input v-model="form.name"></el-input>
                        </el-form-item>
                        <el-form-item label="排序">
                            <el-input v-model="form.display_order" type="number" ></el-input>
                        </el-form-item>
                        <el-form-item label="分类类型">
                            <el-select v-model="form.type" clearable placeholder="类型">
                                <el-option label="文章" :value="0"></el-option>
                                <el-option label="产品" :value="1"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="状态">
                            <template>
                                <el-switch
                                        v-model="form.status"
                                        :active-value="1"
                                        :inactive-value="0"
                                >
                                </el-switch>
                            </template>
                        </el-form-item>
                        <el-form-item label="缩略图" prop="thumb">
                            <div class="upload-box" @click="openUpload('thumb')" v-if="!form.thumb">
                                <i class="el-icon-plus" style="font-size:32px"></i>
                            </div>
                            <div @click="openUpload('thumb')" class="upload-boxed" v-if="form.thumb">
                                <img :src="form.thumb" alt="" style="width:150px;height:150px;border-radius: 5px;cursor: pointer;">
                                <div class="upload-boxed-text">点击重新上传</div>
                            </div>
                        </el-form-item>
                        <el-form-item label="描述" prop="description">
                            <el-input v-model="form.description" :rows="5" type="textarea" ref="seo_description"></el-input>
                        </el-form-item>
                    </div>
                </div>
                <div class="vue-head">
                    <div class="vue-main-title">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content">SEO信息</div>
                    </div>
                    <div class="vue-main-form">
                        <el-form-item label="SEO标题" prop="seo_title">
                            <el-input v-model="form.seo_title" ref="seo_title"></el-input>
                        </el-form-item>
                        <el-form-item label="SEO关键词" prop="seo_keywords">
                            <el-input v-model="form.seo_keywords" ref="seo_keywords"></el-input>
                        </el-form-item>
                        <el-form-item label="SEO描述" prop="seo_description">
                            <el-input v-model="form.seo_description" :rows="5" type="textarea" ref="seo_description"></el-input>
                        </el-form-item>
                    </div>
                </div>
                <div class="vue-head">
                    <div class="vue-main-title">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content">模版设置</div>
                    </div>
                    <div class="vue-main-form">
                        <el-form-item label="PC列表模版">
                            <el-select v-model="form.pc_list_tpl" clearable placeholder="">
                                <el-option v-for="(item,index) in pc_list_tpl_list" :key="index" :label="item.file" :value="item.file"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="PC详情模版">
                            <el-select v-model="form.pc_detail_tpl" clearable placeholder="">
                                <el-option v-for="(item,index) in pc_detail_tpl_list" :key="index" :label="item.file" :value="item.file"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="手机端列表模版">
                            <el-select v-model="form.h5_list_tpl" clearable placeholder="">
                                <el-option v-for="(item,index) in h5_list_tpl_list" :key="index" :label="item.file" :value="item.file"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="手机端详情模版">
                            <el-select v-model="form.h5_detail_tpl" clearable placeholder="">
                                <el-option v-for="(item,index) in h5_detail_tpl_list" :key="index" :label="item.file" :value="item.file"></el-option>
                            </el-select>
                        </el-form-item>
                    </div>
                </div>
            </el-form>
            <div class="vue-page">
                <div class="vue-center">
                    <el-button type="primary" @click="submitForm('form')">提交</el-button>
                    <el-button @click="goBack">返回</el-button>
                </div>
            </div>
            <upload-img :upload-show="uploadShow" :name="chooseImgName" @replace="changeProp" @sure="sureImg"></upload-img>
        </div>

    </div>
    @include('backend.public.uploadImg')
    <script>
        var app = new Vue({
            el:"#app",
            delimiters: ['[[', ']]'],
            name: 'test',
            data() {
                return{
                    id:0,
                    form:{
                        pid:'',
                        status:1,
                        display_order:'',
                        name:'',
                        type:0,
                        thumb:'',
                        description:'',
                        seo_title:'',
                        seo_keywords:'',
                        seo_description:'',
                        pc_list_tpl:'',
                        pc_detail_tpl:'',
                        h5_list_tpl:'',
                        h5_detail_tpl:''
                    },
                    pc_list_tpl_list:[],
                    pc_detail_tpl_list:[],
                    h5_list_tpl_list:[],
                    h5_detail_tpl_list:[],
                    parent_list:[],
                    rules:{
                        name:{ required: true, message: '请输入名称'}
                    },
                    submit_url:'',
                    uploadShow:false,
                    chooseImgName:'',

                }
            },
            created() {

            },
            mounted() {
                this.id = getParam("id");
                if(this.id) {
                    this.is_edit = true;
                    this.submit_url = `{!! absolute_admin_url('content.category.update') !!}`;

                }
                else {
                    this.submit_url = `{!! absolute_admin_url('content.category.create') !!}`;
                }
                this.getParent();
                this.getData();
            },
            methods: {
                getParent() {
                    let params = {};
                    if(this.id){
                        params['except_id'] = this.id;
                    }
                    params['type'] = -1;
                    this.$http.post('{!! absolute_admin_url('content.category.query') !!}',params).then(function(response) {
                        if (response.data.result) {
                            this.parent_list = response.data.data;
                        } else {
                            this.$message({
                                message: response.data.msg,
                                type: 'error'
                            });
                        }
                    }, function(response) {
                        this.$message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    });
                },
                goParent() {
                    window.location.href = `{!! absolute_admin_url('content.category.index') !!}`;
                },
                getData() {
                    let json = {};
                    if(this.id){
                        json['id'] = this.id;
                    }
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url('content.category.getDetail') !!}',json).then(function (response) {
                            if (response.data.result){
                                if(response.data.data.info && response.data.data.info['name']){
                                    this.form = {
                                        ...response.data.data.info,
                                    };
                                }
                                this.pc_list_tpl_list=response.data.data.pc_list_tpl_list;
                                this.pc_detail_tpl_list=response.data.data.pc_detail_tpl_list;
                                this.h5_list_tpl_list=response.data.data.h5_list_tpl_list;
                                this.h5_detail_tpl_list=response.data.data.h5_detail_tpl_list;
                            }
                            else {
                                this.$message({message: response.data.msg,type: 'error'});
                            }
                            loading.close();
                        },function (response) {
                            this.$message({message: response.data.msg,type: 'error'});
                            loading.close();
                        }
                    );
                },
                submitForm(formName) {
                    let that = this;
                    let json = {
                        pid:this.form.pid,
                        status:this.form.status,
                        display_order:this.form.display_order,
                        name:this.form.name,
                        type:this.form.type,
                        thumb:this.form.thumb,
                        description:this.form.description,
                        seo_title:this.form.seo_title,
                        seo_keywords:this.form.seo_keywords,
                        seo_description:this.form.seo_description,
                        pc_list_tpl:this.form.pc_list_tpl,
                        pc_detail_tpl:this.form.pc_detail_tpl,
                        h5_list_tpl:this.form.h5_list_tpl,
                        h5_detail_tpl:this.form.h5_detail_tpl
                    };
                    if(this.id) {
                        json.id = this.id
                    }
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                            this.$http.post(this.submit_url,{params:json}).then(response => {
                                if (response.data.result) {
                                    this.$message({type: 'success',message: '操作成功!'});
                                    this.goParent();
                                } else {
                                    this.$message({message: response.data.msg,type: 'error'});
                                }
                                loading.close();
                            },response => {
                                loading.close();
                            });
                        }
                        else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                },

                goBack() {
                    history.go(-1)
                },
                openUpload(str) {
                    this.chooseImgName = str;
                    this.uploadShow = true;
                },
                changeProp(val) {
                    if(val == true) {
                        this.uploadShow = false;
                    }
                    else {
                        this.uploadShow = true;
                    }
                },
                sureImg(name,image,image_url) {
                    this.form[name] = image_url;
                },
                clearImg(str) {
                    this.form[str] = "";
                    this.$forceUpdate();
                },
            },
        })

    </script>
@endsection
